#include <REGX52.H>
#include "LCD1602.h"
#include "Key.h"
#include "Delay.h"
#include "I2C.h"
#include "AT24C02.h"

//unsigned char Data;
unsigned char KeyNum;
unsigned int Num;


void main()
{
	LCD_Init();
	LCD_ShowNum(1,1,Num,5);	//默认上电显示
//	LCD_ShowString(1,1,"Hello");
//	AT24C02_WriteByte(1,147);//第一位指定地址（0-255），第二位指定数据
//	Delay(5);
//	Data = AT24C02_ReadByte(1);//写周期，需要5ms，要延时   //先写入再读取，掉电不丢失
//	LCD_ShowNum(2,1,Data,3);
	while(1)
	{
		KeyNum = Key();
		if(KeyNum == 1)
		{
			Num++;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum == 2)
		{
			Num--;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum == 3)
		{
			AT24C02_WriteByte(0,Num%256);//Num%256是取出低八位
			Delay(5);
			AT24C02_WriteByte(1,Num/256);//Num/256是取出高八位
			Delay(5);
			LCD_ShowString(2,1,"Write OK");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
		if(KeyNum == 4)
		{
			Num = AT24C02_ReadByte(0);		//0代表取出低八位
			Num |= AT24C02_ReadByte(1)<<8;  //1代表取出高把位，左移8位 与 上一个NUm
			LCD_ShowNum(1,1,Num,5);
			LCD_ShowString(2,1,"Read OK ");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
	}
	
}